package hausaufgaben;

import java.io.*;
import java.util.*;

public class Flugzeug
{
  private String[][] seats;
  
  public Flugzeug()
  {
    seats = new String[7][6];
    for(int i = 0; i<seats.length; i++)
    {
      for(int j = 0; j<seats[i].length; j++)
      {
        if((i==0 || i==1 || i==2) && (j==2 || j ==3))
          seats[i][j] = " ";
        else
          seats[i][j] = (char)('A'+j)+"";
      }
    }
  }
  
  public void belege(String a)
  {
    int row = a.charAt(0)-'0';
    int seat = Character.toUpperCase(a.charAt(1))-'A'+1;
    
    if(row<1 || row >7 || seat<1 || seat>6)
      try
      {
        throw new WrongPositionException("Position does not exist.");
      }
      catch (WrongPositionException e)
      {
        e.printStackTrace();
      }
    else if(seats[row-1][seat-1].contains("X"))
      try
      {
        throw new NotAvailableException();
      }
      catch (NotAvailableException e)
      {
        e.printStackTrace();
      }
    else
      seats[row-1][seat-1] = "X";
  }
  
  public void speichern()
  {
    File f = new File("sitzplan.txt");
    PrintWriter pw = null;
    try
    {
      pw = new PrintWriter(f);
    }
    catch (FileNotFoundException e)
    {
      System.out.println("File "+f+" not found.");
      System.exit(1);
    }
    pw.print(toString());
    pw.close();
  }
  
  public ArrayList<Integer> findeReihen(int personen)
  {
    ArrayList<Integer> possibleRows = new ArrayList<Integer>();
    
    for(int i = 0; i<seats.length; i++)
    {
      int count = 0;
      for(int j = 0; j<seats[i].length; j++)
      {
        if(!seats[i][j].contains("X") && !seats[i][j].trim().isEmpty())
          count++;
        else if(seats[i][j].contains("X"))
          count = 0;
      }
      if(count>=personen)
        possibleRows.add(i+1);
    }
    return possibleRows;
  }
  
  public String toString()
  {
    String output = "";
    for(int i = 0; i<seats.length; i++)
    {
      output += i+1+" ";
      for(int j = 0; j<seats[i].length; j++)
      {
        output += " "+seats[i][j];
      }
      output += "\n";
    }
    return output;
  }
}
